package com.yn.club.subject.application.controller;



import com.yn.club.subject.domain.entity.SubjectCategoryDO;
import com.yn.club.subject.application.dto.SubjectCategoryDTO;
import com.yn.club.subject.domain.service.SubjectCategoryDomainService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.subject.application.convert.SubjectCategoryApplicationConverter;
import com.yn.club.subject.application.request.SubjectCategoryQueryRequest;
import com.yn.club.subject.application.request.SubjectCategorySaveRequest;

/**
 * 题目分类(SubjectCategory)表控制层
 *
 * @author YN
 * @since 2025-01-21 17:40:47
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("subjectCategory")
public class SubjectCategoryController {
    /**
     * 服务对象
     */
    private final SubjectCategoryDomainService subjectCategoryDomainService;
    private final SubjectCategoryApplicationConverter subjectCategoryApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping
    public R<PageResp<SubjectCategoryDTO>> queryPage(PageReq pageReq, SubjectCategoryQueryRequest query) {
        SubjectCategoryDO subjectCategoryDO = subjectCategoryApplicationConverter.convertQueryRequestToDo(query);
        PageResp<SubjectCategoryDO> page = subjectCategoryDomainService.page(pageReq, subjectCategoryDO);
        
        List<SubjectCategoryDTO> subjectCategoryDTOS = subjectCategoryApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,subjectCategoryDTOS));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<SubjectCategoryDTO> selectOne(@PathVariable Serializable id) {
    SubjectCategoryDTO subjectCategoryDTO = subjectCategoryApplicationConverter.convertDoToDto(
      this.subjectCategoryDomainService.getById(id));
     
    return R.ok(subjectCategoryDTO);
    }

    /**
     * 新增数据
     *
     * @param subjectCategory 实体对象
     * @return 受影响条数
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody SubjectCategorySaveRequest subjectCategory) {
       SubjectCategoryDO subjectCategoryDO = subjectCategoryApplicationConverter.convertSaveRequestToDo(subjectCategory);
        return R.ok(this.subjectCategoryDomainService.insert(subjectCategoryDO));
    }

    /**
     * 修改数据
     *
     * @param subjectCategory 实体对象
     * @return 受影响条数
     */
    @PutMapping
    public R<Boolean> update(@RequestBody SubjectCategorySaveRequest subjectCategory) {
        SubjectCategoryDO subjectCategoryDO = subjectCategoryApplicationConverter.convertSaveRequestToDo(subjectCategory);
        return R.ok(this.subjectCategoryDomainService.updateById(subjectCategoryDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.subjectCategoryDomainService.delete(ids));
    }
}

